#include "scut.h"
#include <QEventLoop>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtWidgets/QWidget>
#include <QTextCodec>
#include <QString>
#include <QUrl>
#include <QJsonParseError>
#include <QJsonObject>
#include <QRegExp>
#include <QRegularExpression>



scut::scut(database * Tempdb)
{
   db=Tempdb;
}




//华南理工大学
QString scut::search_next_page(QString data)
{

    QString i;

    QStringList linshi;

    QStringList temp;

    i=QString("(.*)下一页(.*)");

    linshi=search(i,data);

    if(linshi.size()>0)
    {
        temp = search("(.*)href=\"",linshi[0]);

        linshi[0]=linshi[0].replace(temp[0],"");

        temp = search("\" target(.*)",linshi[0]);

        linshi[0]=linshi[0].replace(temp[0],"");

        //qDebug()<<linshi[0];

        return linshi[0];

    }

    else

    {
        qDebug()<<"没有找到上下页";

        return "";

    }

}

QString scut::search_final_page(QString data)
{

    QString i;

    QStringList linshi;

    QStringList temp;

    i=QString("(.*)尾页(.*)");

    linshi=search(i,data);

    if(linshi.size()>0)
    {
        temp = search("(.*)href=\"",linshi[0]);

        linshi[0]=linshi[0].replace(temp[0],"");

        temp = search("\" target(.*)",linshi[0]);

        linshi[0]=linshi[0].replace(temp[0],"");

        //qDebug()<<linshi[0];

        return linshi[0];

    }

    else

    {
        qDebug()<<"没有找到尾页";

        return "";

    }

}

QStringList scut::search_url(QString data)
{

    QString i=QString(" <li class=\"news_li\"> (.*)");

    QStringList linshi;

    linshi=search(i,data);

    //删掉非讲座信息

    i=QString("学术");

    for (int a=0;a<linshi.length();a++) {

        if(search(i,linshi[a]).size()<=0)
        {

            linshi.removeOne(linshi[a]);

            a--;

        }

    }


    //移除非网址部分

    i=QString("(.*)<a href='");

    QString i2=QString("' target(.*)");

    QStringList useless_part;

    for(int a=0;a<linshi.length();a++)

    {

        //qDebug()<<linshi[a];

        useless_part = search(i,linshi[a]);

        linshi[a].replace(useless_part[0],"");

        useless_part = search(i2,linshi[a]);

        linshi[a].replace(useless_part[0],"");

    }

    return linshi;

}

QStringList scut::search_date(QString data)
{
    QString i=QString(" <li class=\"news_li\"> (.*)");

    QStringList linshi;

    linshi=search(i,data);

    //删掉非讲座信息

    i=QString("学术");

    for (int a=0;a<linshi.length();a++) {

        if(search(i,linshi[a]).size()<=0)
        {

            linshi.removeOne(linshi[a]);

            a--;

        }

    }

    //移除非日期部分

    i=QString("(.*)news_meta\">");

    QString i2=QString("</span> </li>\r");

    QStringList useless_part;

    for(int a=0;a<linshi.length();a++)

    {

        //qDebug()<<linshi[a];

        useless_part = search(i,linshi[a]);

        linshi[a].replace(useless_part[0],"");

        useless_part = search(i2,linshi[a]);

        linshi[a].replace(useless_part[0],"");

    }

    return linshi;
}

QStringList scut::search_data(QString data)
{

    //抓取论文信息

    QStringList title,lecturer,time,place,university,url;

    QStringList useless_part;

    useless_part<<"&nbsp;"<<"<(.*?)>";

    QStringList temp;

    //处理信息

    QString regulardata="<div class='wp_articlecontent'>(.*)";

    data = search(regulardata,data)[0];

    if(data.size()>0)
    {
        for(int b=0;b<useless_part.size();b++)
        {
            temp = search(useless_part[b],data);
            if(temp.size()>0)
                for(int c=0;c<temp.size();c++)
                    data.replace(temp[c],"");
        }
    }

    //查找标题

    regulardata="题目(.*?)报";

    QStringList searchdata=search(regulardata,data);

    if(searchdata.size()>0)

    {
        //删去无效信息
        for(int a=0;a<searchdata.size();a++)
        {
            temp = search("题目(.*)：",searchdata[a]);
            searchdata[a].replace("报","");
            searchdata[a].replace(temp[0],"");

            title.append(searchdata[a]);
        }

    }

    else
    {
        qDebug()<<"查找标题时发生错误";

        title.append("出错\r");
    }

    //qDebug()<<title;


    //获取报告人

    regulardata="报告人：(.*?)主";

    searchdata=search(regulardata,data);

    if(searchdata.size()>0)

    {
        for(int a=0;a<searchdata.size();a++)
        {
            searchdata[a].replace("主","");
            searchdata[a].replace("报告人：","");

            lecturer.append(searchdata[a]);
        }

    }

    else
    {
        searchdata=search("报 告 人：(.*?)主",data);

        if(searchdata.size()>0)

        {
            for(int a=0;a<searchdata.size();a++)
            {
                searchdata[a].replace("主","");
                searchdata[a].replace("报 告 人：","");

                lecturer.append(searchdata[a]);
            }

        }

        else {

            searchdata=search("简介：(.*?)，",data);

            if(searchdata.size()>0)

            {
                for(int a=0;a<searchdata.size();a++)
                {
                    searchdata[a].replace("简介：","");
                    searchdata[a].replace("，","");

                    lecturer.append(searchdata[a]);
                }

            }

            else{
            qDebug()<<"查找报告人失败";

            lecturer.append("出错\r");
            }
        }

    }

    //qDebug()<<lecturer;


    //查找时间

    regulardata="时间(.*?)地点";

    searchdata=search(regulardata,data);

    if(searchdata.size()>0)

    {

        for(int a=0;a<searchdata.size();a++)
        {
            searchdata[a].replace("报告地点","");
            searchdata[a].replace("地点","");
            searchdata[a].replace("时间：","");

            searchdata[a] = date_form(searchdata[a]);
            time.append(searchdata[a]);
        }

    }

    else

    {
        searchdata=search("时间(.*?)会",data);
        if(searchdata.size()>0)

        {

            for(int a=0;a<searchdata.size();a++)
            {

                searchdata[a].replace("时间：","");
                searchdata[a].replace("会","");

                searchdata[a] = date_form(searchdata[a]);
                time.append(searchdata[a]);
            }

        }


        else
        {

        qDebug()<<"查找时间失败";

        time.append("出错\r");
        }
    }


    //qDebug()<<time;

    //查找地点

    regulardata="地点(.*?)摘要";

    searchdata=search(regulardata,data);

    if(searchdata.size()>0)

    {
        for(int a=0;a<searchdata.size();a++)
        {
            searchdata[a].replace("报告摘要","");
            searchdata[a].replace("摘要","");
            searchdata[a].replace("地点：","");

            place.append(searchdata[a]);
        }

    }

    else

    {
        searchdata=search("地点(.*?)报告",data);

        if(searchdata.size()>0)

        {
            for(int a=0;a<searchdata.size();a++)
            {
                searchdata[a].replace("报告","");
                searchdata[a].replace("地点：","");

                place.append(searchdata[a]);
            }

        }

        else{

            searchdata=search("腾讯(.*?)会议主题",data);

            if(searchdata.size()>0)

            {
                for(int a=0;a<searchdata.size();a++)
                {
                    searchdata[a].replace("会议主题","");

                    place.append(searchdata[a]);
                }

            }
            else{

        qDebug()<<"查找地址失败";

        place.append("出错\r");
        }

        }
    }

    //qDebug()<<place;


    QStringList senddata;

    for(int a=0;a<title.length();a++)
    {
        senddata.append(title[a]);

        senddata.append(lecturer[a<lecturer.size()?a:lecturer.size()-1]);

        senddata.append(time[a<time.size()?a:time.size()-1]);

        senddata.append(place[a<place.size()?a:place.size()-1]);

    }


    //qDebug()<<senddata;

    return senddata;

}



bool scut::dealing(){

    //QSqlQuery query(db->getDataBase());

    //获取当前年份
    current_date =QDate::currentDate();


    QSqlQuery query(db->getDataBase());


    query.exec("SELECT link FROM information_list where college='华南理工大学'");

    query.next();//这里指向的是第一条信息
    last_lecture=query.value(0).toString();




    starturl="http://www2.scut.edu.cn";

    weburl="/sse/xshd/list.htm";

    indexpage=get(starturl+weburl);

    nexturl=search_next_page(indexpage);

    finalurl=search_final_page(indexpage);

    //循环所有页面
    do{
        //获取讲座网页的url
        lecture_list = search_url(indexpage);

        lecture_date_list = search_date(indexpage);

        nexturl=search_next_page(indexpage);

        //读取每个网页的信息
        for(int a=0;a<lecture_list.length();a++)
        {

            lecture_date = QDate::fromString(lecture_date_list[a],"yyyy-MM-dd");

            qDebug()<<lecture_list[a]<<lecture_date;

            if(lecture_list[a]=="/sse/2020/0708/c17280a389464/page.htm")
                continue;

            //如果与之前更新的讲座重复，跳出
            if(last_lecture.contains(lecture_list[a]))
            {
                qDebug()<<"华南理工大学更新完成";
                nexturl="/sse/xshd/list10.htm";
                break;
            }

            //如果时间超过一年,则跳出
            if(lecture_date.daysTo(current_date)>365)
            {
                qDebug()<<"超过一年";
                nexturl="/sse/xshd/list10.htm";
                break;
            }

            lecture_data = search_data(get(starturl+lecture_list[a]));

            //将信息存入txt

            for(int i=0;i<lecture_data.length();i=i+4)
            {
                QString title,lecture,time,place,url;
                title=lecture_data[i];
                lecture=lecture_data[i+1];
                time=lecture_data[i+2];
                place=lecture_data[i+3];
                url=starturl+lecture_list[a];
                db->insert_notice(lecture_date_list[a],url);
                db->insert_report(title,lecture,time,place,"华南理工大学",url);

            }
        }

        indexpage=get(starturl+nexturl);

    }while(nexturl!="/sse/xshd/list10.htm");

    return true;
}
